package demo.gl;



import java.util.Random;


import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.graphics.Bitmap;

public class OpenGLRenderer extends GLSurfaceView implements Renderer,Define{
	private SurfaceHolder holder;
	private int x=0;
	private int y=0;
	private int touchIngamex=0;
	private int touchIngamey=0;
	public static int CursorX;
	public static int CursorY;
	public static boolean showCursor=false;
	public static int currentstate=STATE_INIT;
	private Bitmap bmpMenuOption,bmpMenuPlay,bmpMenuAbout,bmpMenuMore;
	private Bitmap bmpBackground;
	public static Canvas c;
	public static int[][] a=new int[12][12];
	public static int[][] m2c=new int[12][12];
	public static int[] m1c=new int[144];
	public static int[][] a2cfindpath=new int[12][12];
	public static int [][] overlaymatrix=new int[12][12];
	public static int[][] ai2d=new int[12][12];
	public static int[][] ai2dfinal=new int[12][12];
	public static int[][] aimatrix=new int[12][12];
	public static int[][] aitempfinal=new int[12][12];
	public static int[][] ai2drun=new int[12][12];
	public static int numMaxBallCanEat=0;
	public static int[] path=new int[144];
	public static int numEmpty=0;
	public static boolean ismove=false;
	public static int ballChose=65;
	public static int score=0;
	public static int numPath=0;
	public static boolean isPath=false;
	public static boolean isCheckPath=false;
	public static boolean isEat=false;
	public static boolean havepathmax=false;
	public static boolean isAutoplay=false;
	public static long timenow=0;
	public static int xOverlay=0;
	public static int yOverlay=0;
	public static int [][] checkOverlay=new int[12][12];
	public static int [][] ball2=new int[12][12]; 
	public static int [][] ball3=new int[12][12];
	public static int [][] ball4=new int[12][12];
	public static int [][] minBall=new int[12][12];
	public static int [][] tempminBall=new int[12][12];
	public static int xSource;
	public static int ySource;
	public static int [][] minPath=new int[12][12];
	public static int [][] tempminPath=new int[12][12];
	//public static vienbi[] ball1=new vienbi[6];
	private Context context;
	private background bg;
	public float radiusX=0;
	public float radiusY=0;
	public float oldX;
	public float oldY;
	private ball ball1;
	int rowCurrent=-1;
	int columnCurrent=-1;
	int oldRow=-1;
	int oldColumn=-1;
	public OpenGLRenderer(Context newcontext) {
		super(newcontext);
		this.setRenderer(this);
		this.requestFocus();
		this.setFocusableInTouchMode(true);
		this.context=newcontext;
		context=newcontext;
		// TODO Auto-generated constructor stub
	}

	public void onDrawFrame(GL10 gl) {
		// TODO Auto-generated method stub
		// Set the background color to black ( rgba ).
		//add
		switch(currentstate)
		{
			case STATE_INIT:
			{
				//loadData();
				//canvas.drawColor(Color.BLUE);
				/*try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}*/
			}
			case STATE_LOADING:
			{
				loadData();
				gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
				gl.glLoadIdentity();
				gl.glTranslatef(0.0f, 0.0f, -6);
				//canvas.drawColor(Color.BLACK);
				loadData();
				currentstate=STATE_MAINMENU;
				break;
			}
			case STATE_MAINMENU:
			{
				gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
				gl.glLoadIdentity();
				gl.glTranslatef(0.0f, 0.0f, -6);
				//drawMainMenu(gl);
				currentstate=STATE_GAMEPLAY;
				break;
			}
 			case STATE_GAMEPLAY:
			{
				
				gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
				gl.glLoadIdentity();
				gl.glTranslatef(0.0f, 0.0f, -6);	
				gl.glRotatef(radiusX, 0.0f, 1.0f, 0.0f); 
				gl.glRotatef(radiusY, 1.0f, 0.0f, 0.0f);
				drawGame(gl);
				if (isAutoplay==true)
				{
					autoplay();
					updateInGamePlay();
				}
				//drawBall(2,1,1);
				//drawCusor();
				break;
			}
			default:
			{
				gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
				gl.glLoadIdentity();
				gl.glTranslatef(0.0f, 0.0f, -6);
				//canvas.drawColor(Color.BLUE);
				/*if (x < getWidth() - bmp.getWidth()) {
				x+=10;
				}
				else if(x>(getWidth() - bmp.getWidth())&&y<=(getHeight()-bmp.getHeight())){
					x=0;
					y+=20;
			
				}else if(y>(getHeight()-bmp.getHeight())){
					x=0;y=0;
				}
				canvas.drawBitmap(bmp, x, y, null);
				//sprite.onDraw(canvas);*/
				break;
			}
		}
		//add
		

	}
	public void drawMainMenu(GL10 gl)
	{		
		bmpBackground=BitmapFactory.decodeResource(context.getResources(), R.drawable.background);
		Drawbitmap(bmpBackground, gl);
		//c.drawBitmap(bmpBackground, 0,0, null);
		//c.drawBitmap(bmpMenuPlay,resolution.screenWith/6,resolution.screenHeight/2,null);
		//c.drawBitmap(bmpMenuMore,resolution.screenWith/2+30,resolution.screenHeight/2+30,null);
		//c.drawBitmap(bmpMenuOption,resolution.screenWith/6+30,resolution.screenHeight/2+160,null);
		//c.drawBitmap(bmpMenuAbout,resolution.screenWith/2+60,resolution.screenHeight/2+190,null);
	}
	private int[] textures = new int[1];
	public void Drawbitmap(Bitmap bmp,GL10 gl)
	{
		System.out.println("draw hereeeeeeeeeeeeeee");
		gl.glGenTextures(1, textures, 0);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
		bmp.recycle();
	}
	public void drawGame(GL10 gl)
	{
		for (int i=0;i<12;i++)
			for (int j=0;j<12;j++)
			{
				if ((j==columnCurrent) && (i==rowCurrent))
				{
					bg=new background(i,j,2);
					bg.Draw(gl);
				}
				else
				{
					bg=new background(i,j,1);
					bg.Draw(gl);
				}
			}
		//bg=new background(12,12,2);
		//bg.Draw(gl);
		//System.out.println("ughvhvh");
		for (int i=0;i<12;i++)
		{
			for (int j=0;j<12;j++)
			{
				//System.out.print("  "+a[i][j]);
				if (a[i][j]!=0)
				{
					if (a[i][j]>5)
					{
						ball1=new ball(i,j,a[i][j]-5,2);
					}
					else
					{
						ball1=new ball(i,j,a[i][j],1);
					}
					ball1.draw(gl);
				}
			}
			//System.out.println();
		}
	}
	public void update()
	{
		switch(currentstate)
		{
			case STATE_INIT:
			{
				init();
				//currentstate=STATE_LOADING;
				break;
			}
			case STATE_LOADING:
			{
				//loadData();
				break;
			}
			default:
				break;
		}
	}
	public static void init()
	{
		currentstate=STATE_INIT;
	}
	public void loadData()
	{
		
		initdata();
		System.out.println("resourse");
		bmpBackground=BitmapFactory.decodeResource(context.getResources(), R.drawable.background);
		bmpMenuMore=BitmapFactory.decodeResource(getResources(), R.drawable.more);
		bmpMenuOption=BitmapFactory.decodeResource(getResources(), R.drawable.option);
		bmpMenuPlay=BitmapFactory.decodeResource(getResources(), R.drawable.play);
		bmpMenuAbout=BitmapFactory.decodeResource(getResources(), R.drawable.about);
		createTableBegin();
	}
	public int random(int max)
	{
		Random rd = new Random();
		int intNum = rd.nextInt(max);
		return intNum;
	}
	public void initdata()
	{
		numEmpty=0;
		for(int i=0;i<12;i++)
			for (int j=0;j<12;j++)
			{
				a[i][j]=0;
				m1c[i*12+j]=0;
			}
	}
	public void updateTable()
	{
		numEmpty=0;
		for (int i=0;i<12;i++)
		{
			for (int j=0;j<12;j++)
			{
				if (a[i][j]==0)
				{
					m1c[numEmpty]=i*12+j;
					numEmpty++;
				}
			}
		}
	}
	public void randomballBegin()
	{
		updateTable();
		int ranNum=random(numEmpty);
		int ranColor=random(5)+1;
		//System.out.println("Num"+ranNum+"color:"+ranColor);
		//int i1=ranNum/12;
		//int j1=ranNum-(i1*12);
		//System.out.println("i"+i1+"j:"+j1);
		a[m1c[ranNum]/12][m1c[ranNum]%12]=ranColor;
		//outputArray();
	}
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// TODO Auto-generated method stub
		// Sets the current view port to the new size.
		if(height==0){
			height=1;
		}
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		GLU.gluPerspective(gl, 45.0f, (float)width/(float)height, 0.1f, 100.0f);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
		
	}
	public void createTableBegin()
	{
		for (int i=0;i<3;i++)
		{
			randomballBegin();
			randomballappear();
		}
	}
	public void randomballappear()
	{
		updateTable();
		int ranNum=random(numEmpty);
		int ranColor=random(5)+1+5;
		a[m1c[ranNum]/12][m1c[ranNum]%12]=ranColor;
		//System.out.println("color small"+ranColor);
		//outputArray();
	}
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// TODO Auto-generated method stub
		gl.glDisable(GL10.GL_DITHER);	
		gl.glShadeModel(GL10.GL_SMOOTH);
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
		gl.glClearDepthf(1.0f);
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glDepthFunc(GL10.GL_LEQUAL);
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float x=event.getX();
		float y=event.getY();
		
		//System.out.println("AAAAAAAAAAAAAAAAAAAAAA"+x);
		//System.out.println("AAAAAAAAAAAAAAAAAAAAAAY"+y);
		if(event.getAction()==MotionEvent.ACTION_MOVE){
			float dX=x - oldX;
			float dY=y - oldY;
			radiusX += dX*0.2f;
			radiusY += dY*0.2f;
		}
		oldX=x;
		oldY=y;
		if(event.getAction()==MotionEvent.ACTION_DOWN){
			//x,y
			System.out.println("VBBBrow"+oldRow+"column"+oldColumn);
			
			columnCurrent=(int)(((x-63)/30));
			rowCurrent=(int)((y-184)/30);
			System.out.println("AAAAArow"+rowCurrent+"column"+columnCurrent);
			//xu ly
			//if (oldColumn>0 && oldColumn<12 && oldRow>0 && oldRow<12)
			//System.out.println("AAAAAAAAAAAAAABBBBBBBBBBB"+a[oldRow][oldColumn]);
			if (oldColumn>=0 && oldColumn<12 && oldRow>=0 && oldRow<12 && columnCurrent>=0 && columnCurrent<12
					&& rowCurrent>=0 && rowCurrent<12 && a[oldRow][oldColumn]>0 && a[oldRow][oldColumn]<6 
					)
			{
				//System.out.println("row"+rowCurrent+"column"+columnCurrent);
				//System.out.println("row1"+oldRow+"column1"+oldColumn);
				moveball();
				//move
				//updateInGamePlay();
			}
			if ((rowCurrent>=0 && rowCurrent<12) && (columnCurrent>=0 && columnCurrent<12))
			{
				//System.out.println("row"+rowCurrent+"column"+columnCurrent);
				oldRow=rowCurrent;
				oldColumn=columnCurrent;
			}
			//System.out.println("AAAAAAAAAAAAAAAAAAAAAA"+columnCurrent);
			//System.out.println("AAAAAAAAAAAAAAAAAAAAAAY"+rowCurrent);
		}
		return true;
	}
	
	public void updateInGamePlay()
	{
		
		checkeat();
		if (isEat==false)
		{
		for (int i=0;i<3;i++)
		{
			
			for (int k=0;k<12;k++)
				for (int j=0;j<12;j++)
				{
					if (a[k][j]>5)
					{
						a[k][j]=a[k][j]-5;
					}
				}
			
		}
		for (int i=0;i<1;i++)
		{
			randomballappear();
		}
		}
		else
		{
			isEat=false;
		}
	}
	public void checkeat()
	{
		System.out.println("check");
		for (int i=0;i<12;i++)
			for (int j=0;j<12;j++)
			{
				m2c[i][j]=0;
			}
		
		for (int i=0;i<12;i++)
			for (int j=0;j<12;j++)
			{
				if (a[i][j]!=0)
				{
				//ngang
				int countBall=0;
				if (j<8)
				{
				for (int k=1;k<5;k++)
				{
					//System.out.println("into");
					if (a[i][j]==a[i][j+k])
					{
						countBall++;
						System.out.println("Count:"+countBall);
					}
					else
					{
						break;
					}
				}
				if (countBall==4)
				{
					for (int k=0;k<5;k++)
					{
						m2c[i][j+k]=1;
					}
				}
				}
				//doc
				if (i<8)
				{
				countBall=0;
				for (int k=1;k<5;k++)
				{
					if (a[i+k][j]==a[i][j])
					{
						countBall++;
					}
					else
					{
						break;
					}
				}
				if (countBall==4)
				{
					for (int k=0;k<5;k++)
					{
						m2c[i+k][j]=1;
					}
				}
				}
				//cheo phai
				if (i<8 && j<8)
				{
				countBall=0;
				for (int k=1;k<5;k++)
				{
					if (a[i][j]==a[i+k][j+k])
					{
						countBall++;
					}
					else
					{
						break;
					}
				}
				if (countBall==4)
				{
					System.out.println("Cheo");
					for (int k=0;k<5;k++)
					{
						m2c[i+k][j+k]=1;
					}
				}
				}
				
				//cheo trai
				if (i>3 && j<8)
				{
				countBall=0;
				for (int k=1;k<5;k++)
				{
					if (a[i][j]==a[i-k][j+k])
					{
						countBall++;
					}
					else
					{
						break;
					}
				}
				if (countBall==4)
				{
					System.out.println("Cheo");
					for (int k=0;k<5;k++)
					{
						m2c[i-k][j+k]=1;
					}
				}
				}
				
				
				}
		
		//xoa
		}
		for (int i=0;i<12;i++)
			for (int j=0;j<12;j++)
			{
				if (m2c[i][j]==1)
				{
					isEat=true;
					a[i][j]=0;
					score++;
				}
			}
		System.out.println("Score:"+score);
	}
	public void moveball()
	{
		System.out.println("x"+oldX);
		//if (oldX>=0 && oldY>=0)
		//{
			
			//int i=(oldY-resolution.postTabley)/30;
			//int j=(oldX-resolution.postTablex)/30;
			//System.out.println("dong"+i+"cot"+j+"value"+a[i][j]);
			//int newi=(CursorY-resolution.postTabley)/30;
			//int newj=(CursorX-resolution.postTablex)/30;
			//if ((a[i][j]>0) && a[i][j]<=5)
			//{
				ismove=true;
				//oldX=-1;
				//oldY=-1;
				System.out.println("row"+rowCurrent+"column"+columnCurrent);
				System.out.println("row1"+oldRow+"column1"+oldColumn);
				buildmatrixpath(oldRow, oldColumn, rowCurrent, columnCurrent);
				if (isPath==false)
				{
					System.out.println("FAILED");
				}
				if (((a[rowCurrent][columnCurrent]==0) || a[rowCurrent][columnCurrent]>5)&&isPath==true)
				{
					//ballSource(a[i][j],newi,newj);
					//System.out.println("\n Ma mau la :"+a[i][j]+"newi:"+newi+"newj:"+newj);
					a[rowCurrent][columnCurrent]=a[oldRow][oldColumn];
					a[oldRow][oldColumn]=0;
					updateInGamePlay();
					System.out.println("Play here");
					autoplay();
					/*findPathBallMax(2);
					havepathmax=false;
					for (int imaxpath=4;imaxpath>0;imaxpath--)
					{
						System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAA"+imaxpath);
						if (havepathmax==false)
						{
						findPathBallMax(imaxpath,2);
						System.out.println("finish");
						for (int k1=0;k1<=11;k1++)
						{
							for (int k2=0;k2<=11;k2++)
							{
								System.out.print("  "+ai2dfinal[k1][k2]);
							}
							System.out.println("");
						}
					
						}	
						else
						{
							break;
						}
					}*/
				//}
				//if ((a[newi][newj]>0) && a[newi][newj]<=5)
				//{
					//ismove=false;
					//oldX=CursorX;
					//oldY=CursorY;
				//}
				//	System.out.println("calllll");
			}
		//}
		System.out.println("move");
	}
	public void buildmatrixpath(int x1,int y1,int x2,int y2)
	{
		numPath=1;
		isPath=false;
		for (int i=0;i<12;i++)
		for (int j=0;j<12;j++)
		{
			if (i==x1 && j==y1)
			{
				a2cfindpath[i][j]=1;
			}
			else if (a[i][j]==0||a[i][j]>5)
			{
				a2cfindpath[i][j]=0;
			}
			else
			{
				a2cfindpath[i][j]=-1;
			}
		}
		//System.out.println("x"+x1+"y"+y1+"value final"+a2cfindpath[x1][y1]);
		//identifyPath(x1, y1);
		//outputmatrixfindpath();
		//System.out.println("value final"+a2cfindpath[x1][y1]);
		while (isPath==false)
		{
			//System.out.println("value final"+a2cfindpath[x2][y2]);
			if (a2cfindpath[x2][y2]==numPath)
			{
				isPath=true;
				break;
			}
			int oldnumpath=numPath;
			isCheckPath=false;
			for (int i=0;i<12;i++)
				for (int j=0;j<12;j++)
				{
					if (a2cfindpath[i][j]==numPath)
					{
						identifyPath(i, j);
						//System.out.println("update numpath"+numPath);
					}
				}
			
			if (isCheckPath==true)
			{
				numPath++;
			}
			//outputmatrixfindpath();
			if (numPath-oldnumpath==0)
			{
				System.out.println("break here");
				break;
			}
		}
	}
	public void identifyPath(int i,int j)
	{
		
		if (a2cfindpath[i][j]==numPath)
		{
			//System.out.println("cap nhat here");
			if (j<11 && (a2cfindpath[i][j+1]==0))
			{
				//System.out.println("Cap nhat"+i+"cot"+j+"value"+a2cfindpath[i][j+1]);
				a2cfindpath[i][j+1]=numPath+1;
				isCheckPath=true;
			}
			if (j>0 && (a2cfindpath[i][j-1]==0))
			{
				a2cfindpath[i][j-1]=numPath+1;
				//System.out.println("Cap nhat"+i+"cot"+j+"value"+a2cfindpath[i][j-1]);
				isCheckPath=true;
			}
			if (i<11 && (a2cfindpath[i+1][j]==0))
			{
				a2cfindpath[i+1][j]=numPath+1;
				//System.out.println("Cap nhat"+i+"cot"+j+"value"+a2cfindpath[i+1][j]);
				isCheckPath=true;
			}
			if (i>0 && (a2cfindpath[i-1][j]==0))
			{
				a2cfindpath[i-1][j]=numPath+1;
				//System.out.println("Cap nhat"+i+"cot"+j+"value"+a2cfindpath[i-1][j]);
				isCheckPath=true;
			}
			
		}
			
	}
	
	//autoplay
	
	public void autoplay()
	{
		boolean result=false;
		boolean isfind=false;
		int numberball=0;
		//update method to find path
		havepathmax=false;
		for (int imaxpath=4;imaxpath>0;imaxpath--)
		{
			//System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAA");
			if ( imaxpath>1)
			{
				System.out.println("Find here"+imaxpath);
				//findPathBallMax(imaxpath);
				boolean ktloopcolor=true;
				findPathBallMax(imaxpath);
				for (int i=0;i<12;i++)
					for (int j=0;j<12;j++)
					{
						ai2drun[i][j]=ai2d[i][j];
						if (ai2drun[i][j]>0&&ai2drun[i][j]<=5)
						{
							isfind=true;
						}
					}
				if (isfind==true)
				//result=automove(imaxpath);
				while (ktloopcolor && imaxpath>1)
				{
					if (imaxpath<4)
					{
					int k1=4;	
					while (k1>imaxpath)
					{
						findPathBallMax(k1);
						for (int k=0;k<12;k++)
							for (int j=0;j<12;j++)
							{
								if (ai2drun[k][j]!=0 && ai2drun[k][j]<10 && ai2drun[k][j]==ai2d[k][j])
								{
									ai2drun[k][j]=0;
								}
							}
						k1--;
					}
					}
					
					ktloopcolor=false;
					for (int i=0;i<12;i++)
					{
						for (int j=0;j<12;j++)
						{
							if (ai2drun[i][j]>0 && ai2drun[i][j]<=5)
							{
								ktloopcolor=true;
							}
						}
					}
					System.out.println("Number ball calculator:"+numberball);
					result=automove(imaxpath);
					if (result==true)
					{
						ktloopcolor=false;
						//break;
					}
					
					System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARUNMATRIX");
					for (int i=0;i<12;i++)
					{
						for (int j=0;j<12;j++)
						{
							System.out.print("   "+aimatrix[i][j]);
							System.out.print("a"+ai2drun[i][j]);
						}
						System.out.println("");
					}
					System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARUN");
					for (int i=0;i<12;i++)
					{
						for (int j=0;j<12;j++)
						{
							System.out.print("   "+ai2drun[i][j]);
						}
						System.out.println("");
					}
					System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARUN/////////////////");
				}
			}
			else
			{
				numberball=imaxpath+1;
				break;
			}
			if(result==true)
			{
				System.out.println("break here");
				numberball=imaxpath;
				break;
			}
		}
		if(!result)
		{
			movingOneBall();
		}
			
		System.out.println("Number ball:"+numberball);
		//if don't have path
		/*while (numberball>0)
		{
			numberball--;
			findPathBallMax(numberball);
			automove(numberball);
		}
		if (havepathmax==false)
		{
			//dont have path
		}
		else
		{
			//have path
		}*/
	}
	
	public void findPathBallMax(int nmaxball)
	{
		System.out.println("numballhere"+nmaxball);
		havepathmax=false;
		int cellcounter=0;//find cell final
		//for (int i=0;i<)
		for (int i=0;i<12;i++)
			for (int j=0;j<12;j++)
			{
				ai2d[i][j]=0;
			}
		for (int i=0;i<12;i++)
			for (int j=0;j<12;j++)
			{
				//if (a[i][j]!=0)
				{
				//ngang
				int countBall=0;
				int countBall1=0;
				int countBall2=0;
				int countBall3=0;
				if (j<8)
				{
					
				for (int k=1;k<5;k++)
				{
					//System.out.println("into");
					if (a[i][j]==a[i][j+k] && a[i][j]!=0)
					{
						countBall++;
						//System.out.println("Count:"+countBall);
					}
					if (k<4)
					{
					if (a[i][j+1]==a[i][j+k+1] && a[i][j+1]!=0)
					{
						countBall1++;
						//System.out.println("Count:"+countBall);
					}
					}
					if (k<3)
					{
						if (a[i][j+2]==a[i][j+k+2] && a[i][j+2]!=0)
						{
							countBall2++;
							//System.out.println("Count:"+countBall);
						}
					}
					if (k<2)
					{
						if (a[i][j+3]==a[i][j+k+3] && a[i][j+3]!=0)
						{
							countBall3++;
							//System.out.println("Count:"+countBall);
						}
					}
				}
				if (countBall==nmaxball-1) cellcounter=0;
				if (countBall1==nmaxball-1) cellcounter=1;
				if (countBall2==nmaxball-1) cellcounter=3;
				if (countBall3==nmaxball-1) cellcounter=4;
				if ((countBall==nmaxball-1 || countBall1==nmaxball-1 || countBall2==nmaxball-1 ||countBall3==nmaxball-1))
				{
					System.out.println("dfasf"+a[i][j+cellcounter]);
					havepathmax=true;
					for (int k=0;k<5;k++)
					{	
						if (a[i][j+k]==a[i][j+cellcounter])
						{
							ai2d[i][j+k]=a[i][j+cellcounter];
						}
						else if (a[i][j+k]==0)
						{
							ai2d[i][j+k]=10;
						}
						else
						{
							if (ai2d[i][j+k]==0)
							{
								if (a[i][j+k]<6)
								ai2d[i][j+k]=99;
								else
								ai2d[i][j+k]=97;
							}
						}
					}
				}
				}
				
				
				//doc
				if (i<8)
				{
				cellcounter=0;
				countBall=0;
				countBall1=0;
				countBall2=0;
				countBall3=0;
				for (int k=1;k<5;k++)
				{
					if (a[i+k][j]==a[i][j] && a[i][j]!=0)
					{
						countBall++;
					}
					if (k<4)
					{
					if (a[i+1][j]==a[i+1+k][j] && a[i+1][j]!=0)
					{
						countBall1++;
					}
					}
					if (k<3)
					{
						if (a[i+2][j]==a[i+k+2][j] && a[i+2][j]!=0)
						{
							countBall2++;
							//System.out.println("Count:"+countBall);
						}
					}
					if (k<2)
					{
						if (a[i+3][j]==a[i+k+3][j] && a[i+3][j]!=0)
						{
							countBall3++;
							//System.out.println("Count:"+countBall);
						}
					}
				}
				if (countBall==nmaxball-1) cellcounter=0;
				if (countBall1==nmaxball-1) cellcounter=1;
				if (countBall2==nmaxball-1) cellcounter=3;
				if (countBall3==nmaxball-1) cellcounter=4;
				if ((countBall==nmaxball-1 || countBall1==nmaxball-1 || countBall2==nmaxball-1 ||countBall3==nmaxball-1))
				{
					//System.out.println("doc cell"+a[i+cellcounter][j]);
					havepathmax=true;
					for (int k=0;k<5;k++)
					{
						if (a[i+k][j]==a[i+cellcounter][j])
						{
							ai2d[i+k][j]=a[i+cellcounter][j];
						}
						else if (a[i+k][j]==0)
						{
							ai2d[i+k][j]=10;
						}
						else
						{
							if (ai2d[i+k][j]==0)
							{
								if (a[i+k][j]<6)
								ai2d[i+k][j]=99;
								else
								ai2d[i+k][j]=97;
							}
						}
					}
				}
				}
				
				//cheo phai
				if (i<8 && j<8)
				{
				cellcounter=0;
				countBall=0;
				countBall1=0;
				countBall2=0;
				countBall3=0;
				for (int k=1;k<5;k++)
				{
					if (a[i][j]==a[i+k][j+k] && a[i][j]!=0)
					{
						countBall++;
					}
					if (k<4)
					{
						if (a[i+1][j+1]==a[i+1+k][j+1+k] && a[i+1][j+1]!=0)
						{
							countBall1++;
						}
					}
					if (k<3)
					{
						if (a[i+2][j+2]==a[i+k+2][j+k+2] && a[i+2][j+2]!=0)
						{
							countBall2++;
							//System.out.println("Count:"+countBall);
						}
					}
					if (k<2)
					{
						if (a[i+3][j+3]==a[i+k+3][j+k+3] && a[i+3][j+3]!=0)
						{
							countBall3++;
							//System.out.println("Count:"+countBall);
						}
					}
				}
				if (countBall==nmaxball-1) cellcounter=0;
				if (countBall1==nmaxball-1) cellcounter=1;
				if (countBall2==nmaxball-1) cellcounter=3;
				if (countBall3==nmaxball-1) cellcounter=4;
				if ((countBall==nmaxball-1 || countBall1==nmaxball-1 || countBall2==nmaxball-1 ||countBall3==nmaxball-1))
				{
					havepathmax=true;
					//System.out.println("Cheo");
					for (int k=0;k<5;k++)
					{
						if (a[i+k][j+k]==a[i+cellcounter][j+cellcounter])
						{
							ai2d[i+k][j+k]=a[i+cellcounter][j+cellcounter];
						}
						else if (a[i+k][j+k]==0)
						{
							ai2d[i+k][j+k]=10;
						}
						else
						{
							if (ai2d[i+k][j+k]==0)
							{
								if (a[i+k][j+k]<6)
								ai2d[i+k][j+k]=99;
								else
								ai2d[i+k][j+k]=97;
							}
						}
					}
				}
				}
				
				//cheo trai
				if (i>3 && j<8)
				{
					cellcounter=0;
					countBall=0;
					countBall1=0;
					countBall2=0;
					countBall3=0;
				for (int k=1;k<5;k++)
				{
					if (a[i][j]==a[i-k][j+k]  && a[i][j]!=0)
					{
						countBall++;
					}
					if (k<4)
					{
						if (a[i-1][j+1]==a[i-1-k][j+1+k] && a[i-1][j+1]!=0)
						{
							countBall1++;
						}
					}
					if (k<3)
					{
						if (a[i-2][j+2]==a[i-k-2][j+k+2] && a[i-2][j+2]!=0)
						{
							countBall2++;
							//System.out.println("Count:"+countBall);
						}
					}
					if (k<2)
					{
						if (a[i-3][j+3]==a[i-k-3][j+k+3] && a[i-3][j+3]!=0)
						{
							countBall3++;
							//System.out.println("Count:"+countBall);
						}
					}
				}
				if (countBall==nmaxball-1) cellcounter=0;
				if (countBall1==nmaxball-1) cellcounter=1;
				if (countBall2==nmaxball-1) cellcounter=3;
				if (countBall3==nmaxball-1) cellcounter=4;
				if ((countBall==nmaxball-1 || countBall1==nmaxball-1 || countBall2==nmaxball-1 ||countBall3==nmaxball-1))
				{
					havepathmax=true;
					//System.out.println("Cheo");
					for (int k=0;k<5;k++)
					{
						if (a[i-k][j+k]==a[i-cellcounter][j+cellcounter])
						{
							ai2d[i-k][j+k]=a[i-cellcounter][j+cellcounter];
						}
						else if (a[i-k][j+k]==0)
						{
							ai2d[i-k][j+k]=10;
						}
						else
						{
							if (ai2d[i-k][j+k]==0)
							{
								if (a[i-k][j+k]<6)
								ai2d[i-k][j+k]=99;
								else
								ai2d[i-k][j+k]=97;
							}
						}
					}
				}
				}
				
				
				
				}
			}
			/*System.out.println("not run here");
			if(havepathmax==true) 
			{
				outputmatrix();
				System.out.println("run here");
				
			}*/
	}
	
	
	//use color
		public void findPathBallMax(int nmaxball,int color)
		{
			//System.out.println("num"+nmaxball+"color"+color);
			havepathmax=false;
			int cellcounter=0;//find cell final
			//for (int i=0;i<)
			for (int i=0;i<12;i++)
				for (int j=0;j<12;j++)
				{
					ai2dfinal[i][j]=0;
				}
			for (int i=0;i<12;i++)
				for (int j=0;j<12;j++)
				{
					//if (a[i][j]!=0)
					{
					//ngang
					cellcounter=0;
					int countBall=0;
					int countBall1=0;
					int countBall2=0;
					int countBall3=0;
					if (j<8)
					{
						
					for (int k=1;k<5;k++)
					{
						//System.out.println("into");
						if (a[i][j]==a[i][j+k] && a[i][j]!=0)
						{
							countBall++;
							//System.out.println("Count:"+countBall);
						}
						if (k<4)
						{
						if (a[i][j+1]==a[i][j+k+1] && a[i][j+1]!=0)
						{
							countBall1++;
							//System.out.println("Count:"+countBall);
						}
						}
						if (k<3)
						{
							if (a[i][j+2]==a[i][j+k+2] && a[i][j+2]!=0)
							{
								countBall2++;
								//System.out.println("Count:"+countBall);
							}
						}
						if (k<2)
						{
							if (a[i][j+3]==a[i][j+k+3] && a[i][j+3]!=0)
							{
								countBall3++;
								//System.out.println("Count:"+countBall);
							}
						}
					}
					
					if (countBall==nmaxball-1) cellcounter=0;
					if (countBall1==nmaxball-1) cellcounter=1;
					if (countBall2==nmaxball-1) cellcounter=3;
					if (countBall3==nmaxball-1) cellcounter=4;
					int numballfinal=0;
					if (countBall>numballfinal) numballfinal=countBall;
					if (countBall1>numballfinal) numballfinal=countBall1;
					if (countBall2>numballfinal) numballfinal=countBall2;
					if (countBall3>numballfinal) numballfinal=countBall3;
					//System.out.println("abcnumball"+numballfinal);
					if (numballfinal==nmaxball-1 && a[i][j+cellcounter]==color)
					//if ((countBall==nmaxball-1 || countBall1==nmaxball-1 || countBall2==nmaxball-1 ||countBall3==nmaxball-1)&& a[i][j+cellcounter]==color)
					{
						System.out.println("ngang"+a[i][j+cellcounter]);
						havepathmax=true;
						for (int k=0;k<5;k++)
						{	
							if (a[i][j+k]==a[i][j+cellcounter])
							{
								ai2dfinal[i][j+k]=a[i][j+cellcounter];
							}
							else if (a[i][j+k]==0)
							{
								ai2dfinal[i][j+k]=10;
							}
							else
							{
								if (ai2dfinal[i][j+k]==0)
								{
									if (a[i][j+k]<6)
									ai2dfinal[i][j+k]=99;
									else
									ai2dfinal[i][j+k]=100;
								}
							}
						}
					}
					}
					
					
					//doc
					if (i<8)
					{
					cellcounter=0;
					countBall=0;
					countBall1=0;
					countBall2=0;
					countBall3=0;
					for (int k=1;k<5;k++)
					{
						if (a[i+k][j]==a[i][j] && a[i][j]!=0)
						{
							countBall++;
						}
						if (k<4)
						{
						if (a[i+1][j]==a[i+1+k][j] && a[i+1][j]!=0)
						{
							countBall1++;
						}
						}
						if (k<3)
						{
							if (a[i+2][j]==a[i+k+2][j] && a[i+2][j]!=0)
							{
								countBall2++;
								//System.out.println("Count:"+countBall);
							}
						}
						if (k<2)
						{
							if (a[i+3][j]==a[i+k+3][j] && a[i+3][j]!=0)
							{
								countBall3++;
								//System.out.println("Count:"+countBall);
							}
						}
					}
					if (countBall==nmaxball-1) cellcounter=0;
					if (countBall1==nmaxball-1) cellcounter=1;
					if (countBall2==nmaxball-1) cellcounter=3;
					if (countBall3==nmaxball-1) cellcounter=4;
					int numballfinal=0;
					if (countBall>numballfinal) numballfinal=countBall;
					if (countBall1>numballfinal) numballfinal=countBall1;
					if (countBall2>numballfinal) numballfinal=countBall2;
					if (countBall3>numballfinal) numballfinal=countBall3;
					//System.out.println("abcnumball"+numballfinal);
					if (numballfinal==nmaxball-1 && a[i+cellcounter][j]==color)
					//if ((countBall==nmaxball-1 || countBall1==nmaxball-1 || countBall2==nmaxball-1 ||countBall3==nmaxball-1) && a[i+cellcounter][j]==color)
					{
						System.out.println("doc cell"+a[i+cellcounter][j]);
						havepathmax=true;
						for (int k=0;k<5;k++)
						{
							if (a[i+k][j]==a[i+cellcounter][j])
							{
								ai2dfinal[i+k][j]=a[i+cellcounter][j];
							}
							else if (a[i+k][j]==0)
							{
								ai2dfinal[i+k][j]=10;
							}
							else
							{
								if (ai2dfinal[i+k][j]==0)
								{
									if (a[i][j]<6)
									ai2dfinal[i+k][j]=99;
									else
									ai2dfinal[i+k][j]=100;
								}
							}
						}
					}
					}
					
					//cheo phai
					if (i<8 && j<8)
					{
					cellcounter=0;
					countBall=0;
					countBall1=0;
					countBall2=0;
					countBall3=0;
					for (int k=1;k<5;k++)
					{
						if (a[i][j]==a[i+k][j+k] && a[i][j]!=0)
						{
							countBall++;
						}
						if (k<4)
						{
							if (a[i+1][j+1]==a[i+1+k][j+1+k] && a[i+1][j+1]!=0)
							{
								countBall1++;
							}
						}
						if (k<3)
						{
							if (a[i+2][j+2]==a[i+k+2][j+k+2] && a[i+2][j+2]!=0)
							{
								countBall2++;
								//System.out.println("Count:"+countBall);
							}
						}
						if (k<2)
						{
							if (a[i+3][j+3]==a[i+k+3][j+k+3] && a[i+3][j+3]!=0)
							{
								countBall3++;
								//System.out.println("Count:"+countBall);
							}
						}
					}
					if (countBall==nmaxball-1) cellcounter=0;
					if (countBall1==nmaxball-1) cellcounter=1;
					if (countBall2==nmaxball-1) cellcounter=3;
					if (countBall3==nmaxball-1) cellcounter=4;
					int numballfinal=0;
					if (countBall>numballfinal) numballfinal=countBall;
					if (countBall1>numballfinal) numballfinal=countBall1;
					if (countBall2>numballfinal) numballfinal=countBall2;
					if (countBall3>numballfinal) numballfinal=countBall3;
					//System.out.println("abcnumball"+numballfinal);
					if (numballfinal==nmaxball-1 &&a[i+cellcounter][j+cellcounter]==color)
					//if ((countBall==nmaxball-1 || countBall1==nmaxball-1 || countBall2==nmaxball-1 ||countBall3==nmaxball-1)&&a[i+cellcounter][j+cellcounter]==color)
					{
						System.out.println("cheo phai cell"+a[i+cellcounter][j+cellcounter]);
						havepathmax=true;
						//System.out.println("Cheo");
						for (int k=0;k<5;k++)
						{
							if (a[i+k][j+k]==a[i+cellcounter][j+cellcounter])
							{
								ai2dfinal[i+k][j+k]=a[i+cellcounter][j+cellcounter];
							}
							else if (a[i+k][j+k]==0)
							{
								ai2dfinal[i+k][j+k]=10;
							}
							else
							{
								if (ai2dfinal[i+k][j+k]==0)
								{
									if (a[i+k][j+k]<6)
									ai2dfinal[i+k][j+k]=99;
									else
									ai2dfinal[i+k][j+k]=100;
								}
							}
						}
					}
					}
					
					//cheo trai
					if (i>3 && j<8)
					{
						cellcounter=0;
						countBall=0;
						countBall1=0;
						countBall2=0;
						countBall3=0;
					for (int k=1;k<5;k++)
					{
						if (a[i][j]==a[i-k][j+k]  && a[i][j]!=0)
						{
							countBall++;
						}
						if (k<4)
						{
							if (a[i-1][j+1]==a[i-1-k][j+1+k] && a[i-1][j+1]!=0)
							{
								countBall1++;
							}
						}
						if (k<3)
						{
							if (a[i-2][j+2]==a[i-k-2][j+k+2] && a[i-2][j+2]!=0)
							{
								countBall2++;
								//System.out.println("Count:"+countBall);
							}
						}
						if (k<2)
						{
							if (a[i-3][j+3]==a[i-k-3][j+k+3] && a[i-3][j+3]!=0)
							{
								countBall3++;
								//System.out.println("Count:"+countBall);
							}
						}
					}
					if (countBall==nmaxball-1) cellcounter=0;
					if (countBall1==nmaxball-1) cellcounter=1;
					if (countBall2==nmaxball-1) cellcounter=3;
					if (countBall3==nmaxball-1) cellcounter=4;
					int numballfinal=0;
					if (countBall>numballfinal) numballfinal=countBall;
					if (countBall1>numballfinal) numballfinal=countBall1;
					if (countBall2>numballfinal) numballfinal=countBall2;
					if (countBall3>numballfinal) numballfinal=countBall3;
					//System.out.println("abcnumball"+numballfinal);
					if (numballfinal==nmaxball-1 &&a[i-cellcounter][j+cellcounter]==color)
					//if ((countBall==nmaxball-1 || countBall1==nmaxball-1 || countBall2==nmaxball-1 ||countBall3==nmaxball-1)&& a[i-cellcounter][j+cellcounter]==color)
					{
						havepathmax=true;
						System.out.println("cheo trai cell"+a[i-cellcounter][j+cellcounter]);
						//System.out.println("Cheo");
						for (int k=0;k<5;k++)
						{
							if (a[i-k][j+k]==a[i-cellcounter][j+cellcounter])
							{
								ai2dfinal[i-k][j+k]=a[i-cellcounter][j+cellcounter];
							}
							else if (a[i-k][j+k]==0)
							{
								ai2dfinal[i-k][j+k]=10;
							}
							else
							{
								if (ai2dfinal[i-k][j+k]==0)
								{
									if (a[i-k][j+k]<6)
									ai2dfinal[i-k][j+k]=99;
									else
									ai2dfinal[i-k][j+k]=100;
								}
							}
						}
					}
					}
					
					
					
					}
				}
				/*if(havepathmax==true) 
				{
					System.out.println("abcnum"+nmaxball);
					outputmatrixfinal();
				}*/
		}
		
		
	public boolean automove(int n)
	{
		int  arraytmp[][]=new int[12][12];
		boolean result=false;
		int numball=0;
		int num=0;
		int maxnum=0;
		int color=0;
		
		for (int i=0;i<12;i++)
		{
			for (int j=0;j<12;j++)
				{
					if (ai2drun[i][j]!=0 && ai2drun[i][j]<=5)
					{
					num=countNumberBall(ai2drun[i][j]);
					if (num>maxnum)
					{
						maxnum=num;
						color=ai2drun[i][j];
					}
					}
				}
		}						
		if (color==0)
		{
			
		}
		else
		{
			System.out.println("n"+n);
			System.out.println("color"+color);
			findPathBallMax(n, color);
			for (int i=0;i<12;i++)
			{
				for (int j=0;j<12;j++)
				{
					aitempfinal[i][j]=ai2dfinal[i][j];
					arraytmp[i][j]=ai2dfinal[i][j];
				}
				
			}
			System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARUNFINAL");
			for (int i=0;i<12;i++)
			{
				for (int j=0;j<12;j++)
				{
					System.out.print("   "+aitempfinal[i][j]);
				}
				System.out.println("");
			}
			boolean ktline=true;
			
			while (ktline)
			{
				ktline=false;
				for (int i=0;i<12;i++)
				{
					for (int j=0;j<12;j++)
					{
						if (aitempfinal[i][j]==color)
						{
							ktline=true;
						}
					}
				}
				result=movebyColor(n, color);
				if (result==true)
				{
					ktline=false;
				}
				for (int i=0;i<12;i++)
				{
					for (int j=0;j<12;j++)
					{
						if (aitempfinal[i][j]!=0 && aitempfinal[i][j]==aimatrix[i][j])
						{
							aitempfinal[i][j]=0;
						}
					}
				}
				//System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
				for (int i=0;i<12;i++)
				{
					for (int j=0;j<12;j++)
					{
						if (ai2drun[i][j]!=0 && ai2drun[i][j]==aimatrix[i][j])
						{
							ai2drun[i][j]=0;
						}
					}
				}
			}
		}
		System.out.println("enddddddddddddddddddddd");
		for (int i=0;i<12;i++)
		{
			for (int j=0;j<12;j++)
			{
				aitempfinal[i][j]=arraytmp[i][j];
			}
			
		}
		if (result==false)
		{
			movebyColor(n, color);
			for (int i=0;i<12;i++)
			{
				for(int j=0;j<12;j++)
				{
					if (aimatrix[i][j]==100)
					{
						int colormove=a[i][j];
						int countline=n;
						//System.out.println("AAAAAAAAAAAAAAAAMaTRIX");
						
						while(countline>1 && result==false)
						{
							System.out.println("numbal countline"+countline);
							System.out.println("numbal color"+colormove);
							System.out.println("AAAAAAAAAAAAAAAAMaTRIX");
							findPathBallMax(countline, colormove);
							for (int i1=0;i1<12;i1++)
							{
								for(int j1=0;j1<12;j1++)
								{
									System.out.print("  "+ai2dfinal[i1][j1]);
								}
								System.out.println("");
							}
							for (int i1=0;i1<12;i1++)
							{
								for(int j1=0;j1<12;j1++)
								{
									isPath=false;
									if ((ai2dfinal[i1][j1]==99 || ai2dfinal[i1][j1]==10) && (i!=i1) && (j!=j1))
									{
										buildmatrixpath(i, j, i1, j1);
										if (isPath==true)
										{
											System.out.println("AAAAAAAAAAAAAHREEEEEEEEEEEEEEEEEEE");
										}
									}
									if (isPath==true)
									{
										ballmove(i, j, i1, j1);
										result=true;
										break;
									}
									if (result==true)
									{
										break;
									}
								}
							}
							countline--;
						}
					}
					if (result==true)
					{
						break;
					}
				}
				if (result==true)
				{
					break;
				}
			}
		}
		return result;
	}
	
	public int countNumberBall(int color)
	{
		int numberBall=0;
		for (int i=0;i<12;i++)
		{
			for (int j=0;j<12;j++)
				{
					if (a[i][j]==color)
					{
						numberBall++;
					}
				}
		}
		return numberBall;
	}
	
	//move
	public boolean movebyColor(int num,int color)
	{
		boolean result=false;
		System.out.println("Find color");
		
		int i1,j1,line=0;
		for(int i=0;i<12;i++)
		{
			for (int j=0;j<12;j++)
			{
				if (aitempfinal[i][j]==color)
				{
				line=optimizeMatrix(i, j, num);
				System.out.println("is line" +line);
				for(int i2=0;i2<12;i2++)
				{
					for (int j2=0;j2<12;j2++)
					{
						System.out.print("   "+aimatrix[i2][j2]);
					}
					System.out.println();
				}
				break;
				}
			}
			if (line!=0)
			{
				break;
			}
		}
		indentifymatrixai(line, num, color);
		System.out.println("final:");
		for(int i2=0;i2<12;i2++)
		{
			for (int j2=0;j2<12;j2++)
			{
				System.out.print("   "+aimatrix[i2][j2]);
			}
			System.out.println();
		}
		//int typeline=optimizeMatrix(1, 1, num);
		//find all pos center tow cell
		boolean ismoveauto=false;
		for(int i=0;i<12;i++)
		{
			for (int j=0;j<12;j++)
			{
				if (aimatrix[i][j]==99 && a[i][j]!=(color+5))
				{
					if(ballSource(color,i,j))
					{	result=true;
						System.out.println("==========================================");
						ballmove(xSource,ySource,i,j);
						ismoveauto=true;
						checkeat();
						randomballappear();
						break;
					}
				}
			}
			if (ismoveauto)
			{
				break;
			}
		}
		
		//find all pos beside
		if (result==false)
		for(int i=0;i<12;i++)
		{
			for (int j=0;j<12;j++)
			{
				if (aimatrix[i][j]==10)
				{
					
					boolean ktt=optimizePathFinal(aimatrix, i, j, color);
					if (ktt==true)
					{
						if(ballSource(color,i,j))
						{	result=true;
							System.out.println("==========================================");
							ballmove(xSource,ySource,i,j);
							ismoveauto=true;
							checkeat();
							randomballappear();
							break;
						}
					}
				}
			}
			if (ismoveauto)
			{
				break;
			}
		}
		if (result==false)
		for(int i=0;i<12;i++)
		{
			for (int j=0;j<12;j++)
			{
				if (aimatrix[i][j]==10)
				{
					if(ballSource(color,i,j))
					{	result=true;
						System.out.println("==========================================");
						ballmove(xSource,ySource,i,j);
						ismoveauto=true;
						checkeat();
						randomballappear();
						break;
					}
				}
			}
			if (ismoveauto)
			{
				break;
			}
		}
		return result;
	}
	
	
	//normal
	
	public void ballmove(int i,int j,int newi,int newj)
	{
		a[newi][newj]=a[i][j];
		a[i][j]=0;
	}
	
	//exe final matrix
	
	public int optimizeMatrix(int posx,int posy,int number)
	{
		int i1,j1,i2,j2,max=0;
		if (posx>=4) i1=posx-4;
		else i1=0;
		if (posy>=11-4) j1=posy-4;
		else j1=0;
		if (posx<=4) i2=posx+4;
		else i2=11;
		if (posy<=11-4) j2=posy+4;
		else j2=11;
		int num=0;
		int isLine=0;
		//ngang
		resetMatrixai();
		System.out.println("posx="+posx+"posy="+posy+"number="+number);
		System.out.println("i1="+i1+"i2="+i2+"j1="+j1+"j2="+j2);
		for (int i=i1;i<=i2;i++)
		{
			System.out.println("here"+a[posx][posy]);
			if(a[i][posy]==a[posx][posy])
			{
				System.out.println("abc"+i);
				num++;
				aimatrix[i][posy]=a[i][posy];
			}
		}
		if(num>=number)
		{
			isLine=1;
		}
		else
		{
			resetMatrixai();
		}
		if (isLine==0)
		{
			num=0;
			for (int i=j1;i<=j2;i++)
			{
				if(a[posx][i]==a[posx][posy])
				{
					num++;
					aimatrix[posx][i]=a[posx][posy];
				}
			}
			if(num>=number)
			{
				isLine=2;
			}
			else
			{
				resetMatrixai();
			}
		}
		//cheo trai
		if (isLine==0)
		{
			num=0;
			int k1=0;
			int k2=0;
			if (posx-4>=0 && posy-4>=0 )
			{
				k1=-4;
			}
			else
			{
				if (posx-4>=posy-4)
				{
					k1=-posy;	
				}
				else
				{
					k1=-posx;
				}
				//k1=j1;
			}
			
			if (posx+4<=11 && posy+4<=11 )
			{
				k2=4;
			}
			else
			{
				if (posx+4>=posy+4)
				{
					k2=11-posx;	
				}
				else
				{
					k2=11-posy;
				}
				//k1=j1;
			}
			
			System.out.println("K1:"+k1+"k2:"+k2 );
			for (int i=k1;i<=k2;i++)
			{
				if(a[posx+i][posy+i]==a[posx][posy])
				{
					num++;
					aimatrix[posx+i][posy+i]=a[posx][posy];
				}
			}
			if(num>=number)
			{
				isLine=3;
			}
			else
			{
				resetMatrixai();
			}
		}
		//cheo phai
		
		if (isLine==0)
		{
			num=0;
			int k1=0;
			int k2=0;
			System.out.println("posx"+posx+"posy"+posy);
			if (posx-4>=0 && posy+4<=11 )
			{
				k1=-4;
			}
			else if (posx-4>0)
			{
				k1=posy-11;
			}
			else if (posy+4<=11)
			{
				k1=-posx;
			}
			else
			{
				k1=-posx;
			if (posy-k1>11)
			{
				k1=posy-11;
			}
			/*
			if (posx-4<=posy-11)
			{
			k1=-posx; 
			}
			else
			{
			k1=posy-11;
			}*/
			//k1=j1;
			}
			
			if (posx+4<=11 && posy-4>=0 )
			{
				k2=4;
			}
			else
			{
			
				if (posx+4-11>=4-posy)
				{
					k2=11-posx;	
				}
				else
				{
					k2=posy;
				}
				//k1=j1;
			}
			System.out.println("posX:"+posx+"posY:"+posy);
			System.out.println("K11:"+k1+"k22:"+k2);
			
			for (int i=k1;i<=k2;i++)
			{
				System.out.println("posX:"+(posx+i)+"posY:"+(posy-i));
				if(a[posx+i][posy-i]==a[posx][posy])
				{
					num++;
					aimatrix[posx+i][posy-i]=a[posx][posy];
				}
			}
			if(num>=number)
			{
				isLine=4;
			}
			else
			{
				resetMatrixai();
			}
		}
		return isLine;
	}
	public int indentifymatrixai(int isLine,int num,int color)
	{
		int numballempty=0;
		int arraytmp[]=new int[100];
		int imin=999,jmin=999,imax=0,jmax=0;
		for (int i=0;i<10;i++)
		{
			arraytmp[i]=0;
		}
		//System.out.println("color"+color);
		for (int i=0;i<=11;i++)
			for (int j=0;j<=11;j++)
			{
				if (aimatrix[i][j]==color)
				{
					//System.out.println("color hereeeeeeeeeeeeeeeeeeeeeeeeeeeee");
					if (i>imax)
					{
						imax=i;
					}
					if (j>jmax)
					{
						jmax=j;
					}
					if (i<imin)
					{
						imin=i;
					}
					if (j<jmin)
					{
						jmin=j;
					}
				}
			}
		System.out.println("i:"+imin+":"+imax+"j:"+jmin+":"+jmax);
		int lengthx=imax-imin,lengthy=jmax-jmin;
		int tgminx,tgminy,tgmaxx,tgmaxy;
		//doc
		if (isLine==1)
		{
			
				if (imax-4<0)
				{
					tgminx=0;
				}
				else
				{
					tgminx=imax-4;
				}
				if (jmax-4<0)
				{
					tgminy=0;
				}
				else
				{
					tgminy=jmax-4;
				}
				if (imin+5>11)
				{
					tgmaxx=11;
				}
				else
				{
					tgmaxx=imin+5;
				}
				if (jmin+5>11)
				{
					tgmaxy=11;
				}
				else
				{
					tgmaxy=jmin+5;
				}
				System.out.println("max:"+tgmaxx+"min"+tgminx);
				for (int i=tgminx;i<tgmaxx;i++)
				{
					
					if (a[i][jmax]==0)
					{
						for (int k=0;k<5;k++)
						{
							//arraytmp[i-tgminx+k]++;
						}
						aimatrix[i][jmax]=10;
					}
					if (a[i][jmax]>5)
					{
						aimatrix[i][jmax]=99;
					}
					if (a[i][jmax]<5 && a[i][jmax]>0 && a[i][jmax]!=color)
					{
						aimatrix[i][jmax]=100;
					}
			}
			
		}
		//ngang
		if (isLine==2)
		{
			
				if (imax-4<0)
				{
					tgminx=0;
				}
				else
				{
					tgminx=imax-4;
				}
				if (jmax-4<0)
				{
					tgminy=0;
				}
				else
				{
					tgminy=jmax-4;
				}
				if (imin+5>11)
				{
					tgmaxx=11;
				}
				else
				{
					tgmaxx=imin+5;
				}
				if (jmin+5>11)
				{
					tgmaxy=11;
				}
				else
				{
					tgmaxy=jmin+5;
				}
				System.out.println("max:"+tgmaxx+"min"+tgminx);
				for (int i=tgminy;i<tgmaxy;i++)
				{
					
					if (a[imax][i]==0)
					{
						/*for (int k=0;k<5;k++)
						{
							arraytmp[i-tgminx+k]++;
						}*/
						aimatrix[imax][i]=10;
					}
					if (a[imax][i]>5)
					{
						aimatrix[imax][i]=99;
					}
					if (a[imax][i]<5 && a[imax][i]>0 && a[imax][i]!=color)
					{
						aimatrix[imax][i]=100;
					}
			}
			
		}
		//cheo phai
		if (isLine==3)
		{
			
				if (imax-4<0)
				{
					tgminx=0;
				}
				else
				{
					tgminx=imax-4;
				}
				if (jmax-4<0)
				{
					tgminy=0;
				}
				else
				{
					tgminy=jmax-4;
				}
				if (imin+4>11)
				{
					tgmaxx=11;
				}
				else
				{
					tgmaxx=imin+4;
				}
				if (jmin+4>11)
				{
					tgmaxy=11;
				}
				else
				{
					tgmaxy=jmin+4;
				}
				int tgcheomin=0,tgcheomax=0;
				if (tgminx-imin<tgminy-jmin)
				{
					tgcheomin=tgminy-jmin;
				}
				else
				{
					tgcheomin=tgminx-imin;
				}
				if (tgmaxx-imax>tgmaxy-jmax)
				{
					tgcheomax=tgmaxy-jmax+(imax-imin+1);
				}
				else
				{
					tgcheomax=tgmaxx-imax+(imax-imin+1);
				}
				
				System.out.println("max:"+tgmaxx+"min"+tgminx);
				System.out.println("min:"+tgcheomin+"max"+tgcheomax+"hieu"+(imax-imin));
				for (int i=tgcheomin;i<tgcheomax;i++)
				{
					System.out.println(imin+i);
					if (a[imin+i][jmin+i]==0)
					{
						/*for (int k=0;k<5;k++)
						{
							arraytmp[i-tgcheomin]++;
						}*/
						aimatrix[imin+i][jmin+i]=10;
					}
					if (a[imin+i][jmin+i]>5)
					{
						aimatrix[imin+i][jmin+i]=99;
					}
					if (a[imin+i][jmin+i]<5 && a[imin+i][jmin+i]>0 && a[imin+i][jmin+i]!=color)
					{
						aimatrix[imin+i][jmin+i]=100;
					}
			}
			
				
			
		}
		
		//dang fix
		//cheo trai
		//cheo phai
				if (isLine==4)
				{
					int tgcheomin=0,tgcheomax=0;
						if (imax-4>0)
						{
							tgcheomin=imax-imin-4;
						}
						else
						{
							tgcheomin=-imin;
						}
						if(jmax-tgcheomin>11)
						{
							tgcheomin=jmax-11;
						}
						/*if (jmax-4<0)
						{
							tgminy=0;
						}
						else
						{
							tgminy=jmax-4;
						}*/
						if (imin+4>11)
						{
							tgcheomax=11-imin;
						}
						else
						{
							tgcheomax=4;
						}
						if (jmax-tgcheomax<0)
						{
							tgcheomax=jmin;
						}
						/*if (jmin+4>11)
						{
							tgmaxy=11;
						}
						else
						{
							tgmaxy=jmin+4;
						}*/
						
						/*if (tgminx-imin<tgminy-jmin)
						{
							tgcheomin=tgminy-jmin;
						}
						else
						{
							tgcheomin=tgminx-imin;
						}*/
						/*if (tgmaxx-imax>tgmaxy-jmax)
						{
							tgcheomax=tgmaxy-jmax+(imax-imin+1);
						}
						else
						{
							tgcheomax=tgmaxx-imax+(imax-imin+1);
						}*/
						
						//System.out.println("max:"+tgmaxx+"min"+tgminx);
						System.out.println("min:"+tgcheomin+"max"+tgcheomax+"hieu"+(imax-imin));
						for (int i=tgcheomin;i<=tgcheomax;i++)
						{
							System.out.println("i="+i);
							System.out.println("x"+imin+"y"+jmax);
							System.out.println("p"+(imin+i)+"p2"+(jmax-i));
							if (a[imin+i][jmax-i]==0)
							{
								/*for (int k=0;k<5;k++)
								{
									arraytmp[i-tgcheomin]++;
								}*/
								aimatrix[imin+i][jmax-i]=10;
							}
							if (a[imin+i][jmax-i]>5)
							{
								aimatrix[imin+i][jmax-i]=99;
							}
							if (a[imin+i][jmax-i]<5 && a[imin+i][jmax-i]>0 && a[imin+i][jmax-i]!=color)
							{
								aimatrix[imin+i][jmax-i]=100;
							}
					}
				}
				
		//tim phan tu lon nhat trong mang
		for (int i=0;i<10;i++)
		{
			if(arraytmp[i]>numballempty)
			{
				numballempty=arraytmp[i];
			}
		}
		return numballempty;
		
	}
	
	//normal
	public void resetMatrixai()
	{
		for(int i=0;i<12;i++)
		{
			for (int j=0;j<12;j++)
			{
				aimatrix[i][j]=0;
			}
		}
	}
	
	//quang 
	//tham so mau , va toa do diem dich
		public boolean ballSource(int color,int x,int y)
		{	boolean checkBall=false,haveBallone=false,haveBalltwo=false,haveBallfour=false;
			
			int max=0;
			findMinBall(color);
			checkoverlay(color,x,y);
			for(int i=0;i<12;i++)
			for(int j=0;j<12;j++)
			{	
				if(minBall[i][j]!=0)
				{	checkBall=true;
					haveBallone=true;
					haveBalltwo=true;
					haveBallfour=true;
					if(checkOverlay[i][j]>max)
					{
						max=checkOverlay[i][j];
						xSource=i;
						ySource=j;
					}
				}
			}
			
			if(!haveBallone)
			{	
			for(int i=0;i<12;i++)
				for(int j=0;j<12;j++)
				{	
					if(aimatrix[i][j]!=color&&ball2[i][j]==color)
					{
					haveBalltwo=true;
					checkBall=true;
					haveBallfour=true;
						if(checkOverlay[i][j]>max)
						{	
							System.out.println("\n");
							System.out.println("nguyen nam quang :"+ball2[i][j]+"=====:"+ai2d[i][j]);
							max=checkOverlay[i][j];
							xSource=i;
							ySource=j;
						}
					}
				}
			}
			if(!haveBalltwo)
			{
				for(int i=0;i<12;i++)
				for(int j=0;j<12;j++)
				{
					
					if(aimatrix[i][j]!=color&&ball3[i][j]==color)
					{
						checkBall=true;
						haveBallfour=true;
						if(checkOverlay[i][j]>max)
						{
							max=checkOverlay[i][j];
							xSource=i;
							ySource=j;
						}
					}
				}
			}
			if(!haveBallfour)
			{
				for(int i=0;i<12;i++)
				for(int j=0;j<12;j++)
				{	
					if(aimatrix[i][j]!=color&&ball4[i][j]==color)
					{	haveBalltwo=true;
						checkBall=true;
						if(checkOverlay[i][j]>max)
						{	
							max=checkOverlay[i][j];
							xSource=i;
							ySource=j;
						}
					}
				}
			}
			System.out.println("\n");
			System.out.println("Toa do x diem nguon :"+xSource+"Toa do y diem nguon: "+ySource);
			System.out.println("output array of quang");
//			for (int i=0;i<12;i++)
//			{
//				for (int j=0;j<12;j++)
//					{
//						System.out.print("  "+minBall[i][j]);
//					}
//				System.out.println("");
//			}
//			System.out.println("\n output array of quang2 ");
//			for (int i=0;i<12;i++)
//			{
//				for (int j=0;j<12;j++)
//					{
//						System.out.print("  "+ball2[i][j]);
//					}
//				System.out.println("");
//			}
//			System.out.println("\n output array of quang3 ");
//			for (int i=0;i<12;i++)
//			{
//				for (int j=0;j<12;j++)
//					{
//						System.out.print("  "+ball3[i][j]);
//					}
//				System.out.println("");
//			}
//			System.out.println("\n output array of quang4 ");
//			for (int i=0;i<12;i++)
//			{
//				for (int j=0;j<12;j++)
//					{
//						System.out.print("  "+ball4[i][j]);
//					}
//				System.out.println("");
//			}
			if(checkBall)
				return true;
			else 
			{
				System.out.println("-------------------------------------------------");
				return false;
				
			}
		}
		
		public boolean optimizePathFinal(int matrixany[][],int i,int j,int color)
		{
			boolean result=false;
			if (j<11)
			{
			if(matrixany[i][j+1]==color)
			{
				result=true;
			}
			}
			if (j>1)
			{
			if(matrixany[i][j-1]==color)
			{
				result=true;
			}
			}
			if (i<0)
			{
			if(matrixany[i+1][j]==color)
			{
				result=true;
			}
			}
			if (i>0)
			{
			if(matrixany[i-1][j]==color)
			{
				result=true;
			}
			}
			if (i<11 && j>0)
			if(matrixany[i+1][j-1]==color)
			{
				result=true;
			}
			if (i<11 && j<11)
			if(matrixany[i+1][j+1]==color)
			{
				result=true;
			}
			if (j<11 && i>0)
			if(matrixany[i-1][j+1]==color)
			{
				result=true;
			}
			if (j>0 && i>0)
			if(matrixany[i-1][j-1]==color)
			{
				result=true;
			}
			return result;
		}
		
		public void findMinBall(int color)
		{
			findPathBallMax(4,color);
			for(int i=0;i<12;i++)
			{
				for(int j=0;j<12;j++)
				{
					tempminBall[i][j]=ai2dfinal[i][j];
					ball4[i][j]=ai2dfinal[i][j];
					minBall[i][j]=a[i][j];
				}
			}
			for(int i=0;i<12;i++)
			{
				for(int j=0;j<12;j++)
				{
					if(tempminBall[i][j]==minBall[i][j])
					{
						minBall[i][j]=-1;
					}
					
				}
			}
			findPathBallMax(3,color);
			for(int i=0;i<12;i++)
			{
				for(int j=0;j<12;j++)
				{
					tempminBall[i][j]=ai2dfinal[i][j];
					ball3[i][j]=ai2dfinal[i][j];
				}
			}
			for(int i=0;i<12;i++)
			{
				for(int j=0;j<12;j++)
				{
					if(tempminBall[i][j]==minBall[i][j])
					{
						minBall[i][j]=-1;
					}
					
				}
			}
			findPathBallMax(2,color);
			for(int i=0;i<12;i++)
			{
				for(int j=0;j<12;j++)
				{
					tempminBall[i][j]=ai2dfinal[i][j];
					ball2[i][j]=ai2dfinal[i][j];
				}
			}
			for(int i=0;i<12;i++)
			{
				for(int j=0;j<12;j++)
				{
					if(tempminBall[i][j]==minBall[i][j])
					{
						minBall[i][j]=-1;
					}
					
				}
			}
			
			for(int i=0;i<12;i++)
			{	
				System.out.println("\n");
				for(int j=0;j<12;j++)
				{	
					if(minBall[i][j]!=color)minBall[i][j]=0;
				}
			}
//			Loai cac truong cacs day chong cheo nhau
			for(int i=0;i<12;i++)
			{	
				for(int j=0;j<12;j++)
				{	
					if(ball3[i][j]==color)
					{
						ball2[i][j]=0;
						
					}
					if(ball2[i][j]==color)
					{
						ball3[i][j]=0;
						
					}
					if(ball4[i][j]==color)
					{
						ball3[i][j]=0;
						ball2[i][j]=0;
					}
				}
			}
//			System.out.println("---------------------------------------------");
		}
		public void getScore()
		{
		
			int a,b,d,e,f,h=0,tempScore;
			tempScore=score;
			a=tempScore%10;
			viewScore(a,10,10);
			tempScore=tempScore/10;
			b=tempScore%10;
			viewScore(b,10,20);
			tempScore=tempScore/10;
			d=tempScore%10;
			viewScore(d,10,30);
			tempScore=tempScore/10;
			e=tempScore%10;
			viewScore(e,10,30);
			tempScore=tempScore/10;
			f=tempScore%10;
			viewScore(f,10,40);
			
					
		}
		public void viewScore(int h,int xScore, int yScore)
		{
			switch(h)
			{
				case 0:
				{
									
				}
				case 1:
				{
					
				}
				case 2:
				{
					
				}
				case 3:
				{
					
				}
				case 4:
				{
					
				}
				case 5:
				{
					
				}
				case 6:
				{
					
				}
				case 7:
				{
					
				}
				case 8:
				{
					
				}
				case 9:
				{
					
				}
			}
		
		}
		
		public void checkoverlay(int m,int x1,int y1)
		{
				
			int x=-1,y=-1;
			int count=0,minOverlay=0;
			for(int i=0;i<12;i++)
			{
				for (int j=0;j<12;j++)
				if(a[i][j]==m)
				{
					buildmatrixpath(i,j,x1,y1);
				if(isPath)
					overlaymatrix[i][j]=1;
				else 
					overlaymatrix[i][j]=0;
				}
				else if(a[i][j]!=m&&a[i][j]!=0)
					overlaymatrix[i][j]=0;
				else overlaymatrix[i][j]=-1;
			}
			for(int i=0;i<12;i++)
			{
				for (int j=0;j<12;j++)
				{
					if(overlaymatrix[i][j]==1)
					{
						x=i;
						y=j;
					}
					// truong hop o xet do phuc tap o giua ban co
					if(x>=1&&x<=10&&y>=1&&y<=10)
					{
						for(int k=(x-1);k<=(x+1);k++)
						{
							for(int h=(y-1);h<=(y+1);h++)
							if(overlaymatrix[k][h]==0||overlaymatrix[k][h]==1)
							{
								count++;
							}
						}
					}
					// truong hop xet do phuc tap o goc trai tren ban co
					else if(x==0&&y==0)
					{
						if(overlaymatrix[0][1]==0||overlaymatrix[0][1]==1)
							{
								count++;
							}
						if(overlaymatrix[1][1]==0||overlaymatrix[1][1]==1)
							{
								count++;
							}
						if(overlaymatrix[1][0]==0||overlaymatrix[1][0]==1)
							{
								count++;
							}
						count+=6;
					}
					// truong hop xet do phuc tap o goc trai tren ban co
					else if(x==11&&y==0)
					{
						if(overlaymatrix[10][0]==0||overlaymatrix[0][10]==1)
							{
								count++;
							}
						if(overlaymatrix[11][1]==0||overlaymatrix[11][1]==1)
							{
								count++;
							}
						if(overlaymatrix[10][1]==0||overlaymatrix[10][1]==1)
							{
								count++;
							}
						count+=6;
					}
					// truong hop xet do phuc tap o goc phai tren ban
					else if(x==0&&y==11)
					{
						if(overlaymatrix[0][10]==0||overlaymatrix[0][10]==1)
							{
								count++;
							}
						if(overlaymatrix[1][11]==0||overlaymatrix[1][11]==1)
							{
								count++;
							}
						if(overlaymatrix[1][10]==0||overlaymatrix[1][10]==1)
							{
								count++;
							}
						count+=6;
					}
					else if(x==11&&y==11)
					{
						if(overlaymatrix[11][10]==0||overlaymatrix[11][10]==1)
							{
								count++;
							}
						if(overlaymatrix[10][11]==0||overlaymatrix[10][11]==1)
							{
								count++;
							}
						if(overlaymatrix[10][10]==0||overlaymatrix[10][10]==1)
							{
								count++;
							}
						count+=6;
					}
					// xet do phuc tap canh tren
					else if(x==0&&(y>0&&y<11))
					{
						for(int k=0;k<=1;k++)
						{
							for(int h=(y-1);h<=(y+1);h++)
							{
								if(overlaymatrix[k][h]==0||overlaymatrix[k][h]==1)
								{
									count++;
								}
							}
						}
						count+=3;
						
					}
					//xet do phuc tap canh ben trai
					else if(y==0&&(x>0&&x<11))
					{	
						for(int k=(x-1);k<=(x+1);k++)
						{
							for(int h=0;h<=1;h++)
							{
								if(overlaymatrix[k][h]==0||overlaymatrix[k][h]==1)
								{
									count++;
								}
							}
						}
						count+=3;
						
					}
					// xet do phuc tap canh ben phai
					else if(y==11&&(x>0&&x<11))
					{	
						for(int k=(x-1);k<=(x+1);k++)
						{
							for(int h=10;h<=11;h++)
							{
								if(overlaymatrix[k][h]==0||overlaymatrix[k][h]==1)
								{
									count++;
								}
							}
						}
						count+=3;
						
					}
					// xet do phuc tap canh duoi
					else if(x==11&&(y>0&&y<11))
					{	
						for(int k=10;k<=11;k++)
						{
							for(int h=(y-1);h<=(y+1);h++)
							{
								if(overlaymatrix[k][h]==0||overlaymatrix[k][h]==1)
								{
									count++;
								}
							}
						}
						count+=3;
						
					}
					checkOverlay[i][j]=count;
					if(minOverlay<count)
					{
						minOverlay=count;
						xOverlay=i;
						yOverlay=j;
					}
					count=0;
					x=-1;
					y=-1;
						
				}
		
			
			}
			for(int i=0;i<12;i++)
			{
				for(int j=0;j<12;j++)
				{
				
				}
			}
			for(int i=0;i<12;i++)
			{
				System.out.println("\n");
				for(int j=0;j<12;j++)
				System.out.print("  "+checkOverlay[i][j]);
			}
		}
		public void movingOneBall()
	{	
		
		boolean checkball=false;
		int count1=0,count2=0,count3=0,count4=0,count5=0,max=0,colorball=0;
		int x1=0,x2=0,y1=0,y2=0,x2temp=0,y2temp=0;
		for(int i=0;i<12;i++)
		for(int j=0;j<12;j++)
		{
			if(aimatrix[i][j]==10)
			{
				checkball=true;
				break;
			}
		}
		// if(!checkball)
		// {	System.out.println("7777777777777777777777777");
			for(int i=0;i<12;i++)
			for(int j=0;j<12;j++)
			{
				if(a[i][j]==1)
				{
					count1++;
				}
				if(a[i][j]==2)
				{
					count2++;
				}
				if(a[i][j]==3)
				{
					count3++;
				}
				if(a[i][j]==4)
				{
					count4++;
				}
				if(a[i][j]==5)
				{
					count5++;
				}
			}
			if(max<count1)max=count1;
			if(max<count2)max=count2;
			if(max<count3)max=count3;
			if(max<count4)max=count4;
			if(max<count5)max=count5;
			if(max==count1)colorball=1;
			if(max==count2)colorball=2;
			if(max==count3)colorball=3;
			if(max==count4)colorball=4;
			if(max==count5)colorball=5;
		// }
		// else
		// {
			// System.out.println("============da re trkdkdklakknkld==============");
			// return;
		// }
		checkoverlay(colorball,5,5);
		 max=100;
		for(int i=0;i<12;i++)
			for(int j=0;j<12;j++)
			{
				if(a[i][j]==colorball)
				{
					if(checkOverlay[i][j]<max)
					{
					max=checkOverlay[i][j];
					x2temp=i;
					y2temp=j;
					}
				}
			}
	
		System.out.print("\n");
		System.out.println("22222222222x2temp"+x2temp+"y2temp"+y2temp);
		checkoverlay(colorball,x2temp,y2temp);
		max=0;
		for(int i=0;i<12;i++)
			for(int j=0;j<12;j++)
			{
				if(a[i][j]==colorball)
				{
					if(checkOverlay[i][j]>max)
					{
					max=checkOverlay[i][j];
					x1=i;
					y1=j;
					}
				}
			}
		System.out.println("\n");
		System.out.println("33333333333x1"+x1+"y1"+y1);
		
			if(x2temp>=1&&x2temp<=10&&y2temp>=1&&y2temp<=10)
				{
					for(int k=(x2temp-1);k<=(x2temp+1);k++)
					{
						for(int h=(y2temp-1);h<=(y2temp+1);h++)
						if(a[k][h]==0)
						{
							x2=k;
							y2=h;
						}
					}
				}
			else if(x2temp==0&&y2temp==0)
				{
					if(a[0][1]==0)
						{
							x2=0;y2=1;
						}
					else if(a[1][1]==0)
						{
							x2=1;y2=1;
						}
					else if(a[1][0]==0)
						{
							x2=1;y2=0;
						}
				}
			
			else if(x2temp==11&&y2temp==0)
				{
					if(a[10][0]==0)
						{
							x2=10;y2=0;
						}
					else if(a[11][1]==0)
						{
							x2=11;y2=1;
						}
					else if(a[10][1]==0)
						{
							x2=10;y2=0;
						}
				}
				// truong hop xet do phuc tap o goc phai tren ban
				else if(x2temp==0&&y2temp==11)
				{
					if(a[0][10]==0)
						{
							x2=0;y2=10;
						}
					else if(a[1][11]==0)
						{
							x2=1;y2=11;
						}
					if(a[1][10]==0)
						{
							x2=1;y2=10;
						}
				}
				else if(x2temp==11&&y2temp==11)
				{
					if(a[11][10]==0)
						{
							x2=11;y2=10;
						}
					if(a[10][11]==0)
						{
							x2=10;y2=11;
						}
					if(a[10][10]==0)
						{
							x2=10;y2=10;
						}
				}
				// xet do phuc tap canh tren
				else if(x2temp==0&&(y2temp>0&&y2temp<11))
				{
					for(int k=0;k<=1;k++)
					{
						for(int h=(y2temp-1);h<=(y2temp+1);h++)
						{
							if(a[k][h]==0)
							{
								x2=k;y2=h;
							}
						}
					}
					
				}
				//xet do phuc tap canh ben trai
				else if(y2temp==0&&(x2temp>0&&x2temp<11))
				{	
					for(int k=(x2temp-1);k<=(x2temp+1);k++)
					{
						for(int h=0;h<=1;h++)
						{
							if(a[k][h]==0)
							{
								x2=k;y2=h;
							}
						}
					}
					
				}
				// xet do phuc tap canh ben phai
				else if(y2temp==11&&(x2temp>0&&x2temp<11))
				{	
					for(int k=(x2temp-1);k<=(x2temp+1);k++)
					{
						for(int h=10;h<=11;h++)
						{
							if(a[k][h]==0)
							{
								x2=k;y2=h;
							}
						}
					}
					
				}
				// xet do phuc tap canh duoi
				else if(x2temp==11&&(y2temp>0&&y2temp<11))
				{	
					for(int k=10;k<=11;k++)
					{
						for(int h=(y2temp-1);h<=(y2temp+1);h++)
						{
							if(a[k][h]==0)
							{
								x2=k;y2=h;
							}
						}
					}
					
				}

		//=============================
		
	System.out.println("\n");
	System.out.println("quang============================: x1"+x1+"y1:"+y1+"x2 :"+x2+"y2:"+y2);
		buildmatrixpath(x1,y1,x2,y2);
		if(isPath)
			ballmove(x1,y1,x2,y2);
		
	}
	public static int xball1=0;
	public static int yball1=0;
	public void timpi1(int color)
	{		
		int count=0,max=0; 
		for(int i=0;i<12; i++)
		{
			for(int j=0;j<12;j++)
			{
				if(a[i][j]==0){
					checkoverlay(color,i,j);
					for(int h=0;h<12;h++)
					{
						for(int k=0; k<12;k++)
						{
							if(checkOverlay[h][k]!=0)
							{
							count++;
							}
						}
					}
					if(count>max)
					{
					  xball1=i;yball1=j;
					  max=count;
					  count=0;
					 
					}
				}
			}
			
		}
		System.out.println("22222222222x2temp"+xball1+"y2temp"+yball1);
	}

}
